package com.example.parttimejob.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.ReviewsMapper;
import com.example.parttimejob.mapper.StoreMapper;
import com.example.parttimejob.mapper.UserMapper;
import com.example.parttimejob.pojo.entity.Reviews;
import com.example.parttimejob.pojo.entity.dto.ChangeReviewDto;
import com.example.parttimejob.pojo.entity.vo.ReviewUserVo;
import com.example.parttimejob.pojo.entity.vo.ReviewsVo;
import com.example.parttimejob.service.ReviewService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReviewServiceimpl implements ReviewService {

  @Autowired
  private ReviewsMapper reviewsMapper;
  @Autowired
  private UserMapper userMapper;
@Autowired
private StoreMapper storeMapper;

  //商家获取评论//商家查看对用户的评价或者用户对自己的评价   Id为商家Id
    @Override
    public List<ReviewsVo> EvaluateStore(Integer Id,String reviewerCategory) {
        List<ReviewsVo> reviewsVoList=new ArrayList<>();
        // 创建 QueryWrapper 对象
        QueryWrapper<Reviews> queryWrapper = new QueryWrapper<>();
        //商家评论用户
        if (reviewerCategory.equals("商家"))
        {
            // 设置条件，这里假设实体类中的属性为 id
            queryWrapper.eq("reviewer_id", Id).eq("reviewer_category", reviewerCategory);
            List<Reviews> reviews=reviewsMapper.selectList(queryWrapper);
            for (Reviews review : reviews) {
                String Name=userMapper.selectRealByUserId(review.getTargetId());
                ReviewsVo reviewsVo=new ReviewsVo();
                BeanUtils.copyProperties(review,reviewsVo);
                reviewsVo.setRealName(Name);
                reviewsVoList.add(reviewsVo);
            }
        }
        //用户评论商家
        if (reviewerCategory.equals("用户"))
        {
            // 设置条件，这里假设实体类中的属性为 id
            queryWrapper.eq("target_id", Id).eq("reviewer_category", reviewerCategory);
            List<Reviews> reviews=reviewsMapper.selectList(queryWrapper);
            for (Reviews review : reviews) {
                String Name=userMapper.selectRealByUserId(review.getReviewerId());
                ReviewsVo reviewsVo=new ReviewsVo();
                BeanUtils.copyProperties(review,reviewsVo);
                reviewsVo.setRealName(Name);
                reviewsVoList.add(reviewsVo);
            }
        }
        return reviewsVoList;

    }

    //删除评论
    @Override
    public String DeleteStoreView(Integer reviewId) {
        reviewsMapper.deleteById(reviewId);
       return "删除成功!";
    }

    //修改评论信息
    @Override
    public String ChangeReview(ChangeReviewDto changeReviewDto) {
        Reviews reviews=new Reviews();
        BeanUtils.copyProperties(changeReviewDto, reviews);
        reviews.setTimestamps(new Date());
        reviewsMapper.updateById(reviews);
        return "修改评论成功";
    }

    //商家根据jobid获取评论
    @Override
    public ChangeReviewDto StoreGetReviewByReview(Integer ReviewerId ,Integer targetId,String category) {
        QueryWrapper<Reviews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reviewer_id", ReviewerId).eq("target_id", targetId).eq("reviewer_category", category);
        List<Reviews> reviewsList = reviewsMapper.selectList(queryWrapper);
        if (reviewsList != null && reviewsList.size() > 0) {
            ChangeReviewDto changeReviewDto = new ChangeReviewDto();
            BeanUtils.copyProperties(reviewsList.get(0), changeReviewDto);
            return changeReviewDto;
        } else {
            ChangeReviewDto changeReviewDto = new ChangeReviewDto();
            Reviews reviews = new Reviews();
            reviews.setReviewerId(ReviewerId);
            reviews.setTargetId(targetId);
            reviews.setReviewerCategory(category);
            reviewsMapper.insert(reviews);
            BeanUtils.copyProperties(reviews, changeReviewDto);
            System.out.println(changeReviewDto.getReviewId());
            return changeReviewDto;
        }
    }



    //用户查看评论
    @Override
    public List<ReviewUserVo> EvaluateUser(Integer Id, String reviewerCategory) {
        List<ReviewUserVo> reviewUserVoList=new ArrayList<>();
        // 创建 QueryWrapper 对象
        QueryWrapper<Reviews> queryWrapper = new QueryWrapper<>();
        //用户评价商家
        if (reviewerCategory.equals("用户"))
        {
            // 设置条件，这里假设实体类中的属性为 id
            queryWrapper.eq("reviewer_id", Id).eq("reviewer_category", reviewerCategory);
            List<Reviews> reviews=reviewsMapper.selectList(queryWrapper);
            for (Reviews review : reviews) {
                String Name=storeMapper.selectRealByMerchantId(review.getTargetId());
                ReviewUserVo reviewUserVo=new ReviewUserVo();
                BeanUtils.copyProperties(review,reviewUserVo);
                reviewUserVo.setMerchantName(Name);
                reviewUserVoList.add(reviewUserVo);
            }
        }
        //用户评论商家
        if (reviewerCategory.equals("商家"))
        {
            // 设置条件，这里假设实体类中的属性为 id
            queryWrapper.eq("target_id", Id).eq("reviewer_category", reviewerCategory);
            List<Reviews> reviews=reviewsMapper.selectList(queryWrapper);
            for (Reviews review : reviews) {
                String Name=storeMapper.selectRealByMerchantId(review.getReviewerId());
                ReviewUserVo reviewUserVo=new ReviewUserVo();
                BeanUtils.copyProperties(review,reviewUserVo);
                reviewUserVo.setMerchantName(Name);
                reviewUserVoList.add(reviewUserVo);
            }
        }
        return reviewUserVoList;
    }
}
